package winOrDie.Net.Http.httpmsg;

import java.util.ArrayList;

import javax.xml.transform.TransformerException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

/**
 * Todos los m�todos reciben por parametro el Xml devuelto por el servidor web,
 * encuentran el resultado y lo devuelve.
 * @author memic
 *
 */
public class WoD_TreatXml {

	public static boolean getLoginResult(Document XmlResult)
	{
		Element docEle = XmlResult.getDocumentElement();

		NodeList nl = docEle.getElementsByTagName(WoD_Constants.TAG_RESULT);		   

		if (nl != null && nl.getLength() > 0) 
		{	    	   
			Element result = (Element) nl.item(0);
			
			System.out.println(result.getAttribute(WoD_Constants.ATR_NAME));

			if(result.getAttribute(WoD_Constants.ATR_RESULT).equals(WoD_Constants.RESULT_TYPE_OK))
				return true;
			else
				return false;			
		}
		return false;
	}
	
	public static String getNewGameResult(Document XmlResult)
	{
		Element docEle = XmlResult.getDocumentElement();

		NodeList nl = docEle.getElementsByTagName(WoD_Constants.TAG_RESULT);		   

		if (nl != null && nl.getLength() > 0) 
		{	    	   
			Element result = (Element) nl.item(0);

			if(result.getAttribute(WoD_Constants.ATR_RESULT).equals(WoD_Constants.RESULT_TYPE_OK))
				return result.getAttribute(WoD_Constants.ATR_GAMEID);
			else
				return "-1";			
		}
		return "-1";
	}
	
	public static String getUpdateGameResult(Document XmlResult)
	{
		Element docEle = XmlResult.getDocumentElement();

		NodeList nl = docEle.getElementsByTagName(WoD_Constants.TAG_RESULT);		   

		if (nl != null && nl.getLength() > 0) 
		{	    	   
			Element result = (Element) nl.item(0);

			if(result.getAttribute(WoD_Constants.ATR_VALUE).equals(WoD_Constants.RESULT_TYPE_OK))
				return result.getAttribute(WoD_Constants.ATR_GAMEID);
			else
				return "-1";			
		}
		return "-1";
	}
	
	public static String getStatsGameResult(Document XmlResult)
	{
		Element docEle = XmlResult.getDocumentElement();

		NodeList nl = docEle.getElementsByTagName(WoD_Constants.TAG_RESULT);		   

		if (nl != null && nl.getLength() > 0) 
		{	    	   
			Element result = (Element) nl.item(0);

			if(result.getAttribute(WoD_Constants.ATR_VALUE).equals(WoD_Constants.RESULT_TYPE_OK))
				return result.getAttribute(WoD_Constants.ATR_GAMEID);
			else
				return "-1";			
		}
		return "-1";
	}
		
	public static ArrayList<WoD_InfoPartidasActuales> getPartidasResult(Document XmlResult)
	{
		ArrayList<WoD_InfoPartidasActuales> wipas = new ArrayList<WoD_InfoPartidasActuales>();
		Element docEle = XmlResult.getDocumentElement();
		NodeList nl = docEle.getElementsByTagName(WoD_Constants.TAG_PARTIDAS);		   

		try {
			System.out.println("\n RESULT: " + WoD_XmlParsing_Client.ParseXmlDocumentToString(XmlResult));
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if (nl != null && nl.getLength() > 0) 
		{	    	   
			Element result = (Element) nl.item(0);

			NodeList nl2 = result.getElementsByTagName(WoD_Constants.TAG_PARTIDA);
			
			if (nl2 != null && nl2.getLength() > 0) 
			{
				for (int i = 0; i < nl2.getLength(); i++)
				{
					WoD_InfoPartidasActuales wipa = new WoD_InfoPartidasActuales();
					Element partida = (Element) nl2.item(i);
					
					wipa.setGameID(partida.getAttribute(WoD_Constants.ATR_GAMEID));
					wipa.setGameTitle(partida.getAttribute(WoD_Constants.ATR_GAMETITLE));
					wipa.setPlayersNumber(partida.getAttribute(WoD_Constants.ATR_PLAYERSNUMBER));
					wipa.setServerIP(partida.getAttribute(WoD_Constants.ATR_SERVERIP));
					wipa.setServerName(partida.getAttribute(WoD_Constants.ATR_SERVERNAME));					
					wipa.setServerPort(partida.getAttribute(WoD_Constants.ATR_SERVERPORT));
					wipa.setServerStatus(partida.getAttribute(WoD_Constants.ATR_SERVERSTATUS));
					
					wipas.add(wipa);					
				}				
			}			
		}
		return wipas;
	}

	public static WoD_InfoPartidasActuales getPlayersNumberOnGame(Document XmlResult)
	{
		WoD_InfoPartidasActuales wipa = new WoD_InfoPartidasActuales();
		Element docEle = XmlResult.getDocumentElement();
		NodeList nl = docEle.getElementsByTagName(WoD_Constants.TAG_PARTIDAS);		   

		try {
			System.out.println("\n RESULT: " + WoD_XmlParsing_Client.ParseXmlDocumentToString(XmlResult));
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		if (nl != null && nl.getLength() > 0) 
		{	    	   
			Element result = (Element) nl.item(0);

			NodeList nl2 = result.getElementsByTagName(WoD_Constants.TAG_PARTIDA);
			
			if (nl2 != null && nl2.getLength() > 0) 
			{
				for (int i = 0; i < nl2.getLength(); i++)
				{					
					Element partida = (Element) nl2.item(i);
					
					wipa.setPlayersNumber(partida.getAttribute(WoD_Constants.ATR_PLAYERSNUMBER));
					wipa.setPlayersNumber(partida.getAttribute(WoD_Constants.ATR_SERVERSTATUS));
					
					return wipa;		
				}				
			}			
		}
		return wipa;
	}
	
	public static boolean getCheckConnectionResult(Document XmlResult)
	{
		Element docEle = XmlResult.getDocumentElement();
		NodeList nl = docEle.getElementsByTagName(WoD_Constants.TAG_RESULT);		   

		if (nl != null && nl.getLength() > 0) 
		{	    	   
			Element result = (Element) nl.item(0);
			
			if(result.getAttribute(WoD_Constants.ATR_RESULT).equals(WoD_Constants.RESULT_TYPE_OK))
				return true;
			else
				return false;			
		}
		return false;
	}
}
